package ua.org.nuos.sdms.clientgui.client.components.factory;

import com.github.wolfie.blackboard.Event;
import com.vaadin.ui.Button;
import com.vaadin.ui.themes.ChameleonTheme;
import ua.org.nuos.sdms.clientgui.server.AppProxy;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 19.02.12
 * Time: 19:16
 * To change this template use File | Settings | File Templates.
 */
public class ButtonFactory {

    public static Button getButton(String caption, Button.ClickListener listener) {
        Button b = new Button(caption);
        b.setImmediate(true);
        b.addStyleName(ChameleonTheme.BUTTON_SMALL);
        b.addListener(listener);
        return b;
    }

    public static Button getButton(String caption, final String controllerId, Class<? extends Event> eventClass) {
        try {
            final Event ev = eventClass.newInstance();
            return getButton(caption, controllerId, ev);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public static Button getButton(String caption, final String controllerId, final Event firedEvent) {
        return getButton(caption, new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                AppProxy.getInstance().getController(controllerId).fire(firedEvent);
            }
        });
    }

    public static Button getDisabledButton(String caption, final String controllerId, final Event firedEvent) {
        Button b = getButton(caption, controllerId, firedEvent);
        b.setEnabled(false);
        return b;
    }

    public static Button getDisabledButton(String caption, final String controllerId, Class<? extends Event> eventClass) {
        Button b = getButton(caption, controllerId, eventClass);
        b.setEnabled(false);
        return b;
    }

    public static void setButtonColorStyle(Button button, Color color) {
        String style;
        switch (color) {
            case RED:
                style = "red_button";
                break;
            case GREEN:
                style = "green_button";
                break;
            case BLUE:
                style = "blue_button";
                break;
            default:
                style = "default";
        }
        button.addStyleName(style);
    }

    public static enum Color {RED, GREEN, BLUE}
}
